layui.use(['layer', 'form', 'formX', 'setter', 'mynotice', 'admin', 'ax', 'util', 'dataGrid', 'element'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var formX = layui.formX;
    var setter = layui.setter;
    var mynotice = layui.mynotice;
    var admin = layui.admin;
    var $ax = layui.ax;
    var util = layui.util;
    var dataGrid = layui.dataGrid;
    var element = layui.element;
    
    var Cr = {
    	ins: {},
    	getQueryString: (name) => { // 获取url中的参数
    		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
            var r = window.location.search.substr(1).match(reg); 
            if (r != null) return r[2]; 
            return null; 
    	},
    	dataGridRender:() => { // 渲染数据列表
    		var id = Cr.getQueryString('id');
    		if(id == null){
    			id = '';
    		}
    		Cr.ins = dataGrid.render({
    	        elem: '#authorlist',  // 容器
    	        templet: '#rlist',  // 模板
    	        url: '/api/zp/cr/list?id=' + id,  // 数据接口
    	        page: {limit: 20},  // 开启分页
    	        parseData: function(res) {
    	        	if(res.code == 401){
    	        		location.replace('/login');
    	        	}
    	        	return res;
    	        },
    	        done: function(res, curr, count){
    	        	//$('#fansNum').text(count);
    	        }
    	    });
    		dataGrid.on('tool(authorlist)', function (obj) {
                var data = obj.data;
                if (obj.event === 'unfw') {
                	mynotice.load('请稍后');
                    var ajax = new $ax('/api/zp/takeOff/' + data.userId, 'post', function (res) {
                    	mynotice.destroy();
                    	Cr.ins.reload({page: {curr: $(".layui-laypage-em").next().html()}});
            	    }, function (res) {
            	    	mynotice.destroy();
            	    	mynotice.error(res.msg);
            	    });
            	    ajax.start();
                }else if (obj.event === 'fans') {
                	mynotice.load('请稍后');
            	    var ajax = new $ax('/api/zp/follow/' + data.userId, 'post', function (res) {
            	    	mynotice.destroy();
            	    	Cr.ins.reload({page: {curr: $(".layui-laypage-em").next().html()}});
            	    }, function (res) {
            	    	mynotice.destroy();
            	    	mynotice.error(res.msg);
            	    });
            	    ajax.start();
                } 
            });
    	}
    }
    Cr.dataGridRender();
});